package id.co.shuba.spamobile.data;

import java.util.Date;

import id.co.shuba.spamobile.Encryptor;
import id.co.shuba.spamobile.model.UserInfo;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserDB extends BaseDB {	
	
	public UserDB(DatabaseHandler db) {
		super(db);
	}		
	
	private static ContentValues createContentValues(UserInfo info) {
		ContentValues values = new ContentValues();
		values.put("USER_ID", info.getId());
		values.put("AO_CODE", info.getAoCode());
		values.put("NIP", info.getNip());
		values.put("UNIT_CODE", info.getUnitCode());
		values.put("USER_NAME", info.getUserName());
		values.put("PASSWORD", info.getPassword());
		values.put("EMAIL_ADDRESS", info.getEmailAddress());		
		values.put("IMEI", info.getImei());	
		values.put("ENCRYPTION_KEY", info.getEncryptionKey());	
		values.put("LAST_ONLINE", formatFullDate(info.getLastOnline()));	
		values.put("LAST_LOGIN", formatFullDate(info.getLastLogin()));
		values.put("TOKEN", info.getToken());
		values.put("SOURCE_CODE", info.getSourceCode());
		values.put("AO_TYPE", info.getAoType());
		return values;
	}	
	
	public long add(UserInfo info) {
		ContentValues values = createContentValues(info);		
		SQLiteDatabase db = _db.getWritableDatabase();

		return db.insert("TBL_MS_USER", null, values);
	}	
	
	public void addOrUpdate(UserInfo info) {
		SQLiteDatabase db = _db.getWritableDatabase();
		String[] params = new String[] {
				String.valueOf(info.getId()),info.getAoCode(),
				info.getNip(), info.getUnitCode(),info.getUserName(),
				info.getPassword(), info.getEmailAddress(),info.getImei(),info.getEncryptionKey(),
				formatFullDate(info.getLastOnline()),
				formatFullDate(info.getLastLogin()), info.getToken(),info.getSourceCode()
			};
		db.execSQL("INSERT OR REPLACE INTO TBL_MS_USER(USER_ID, AO_CODE, NIP, UNIT_CODE, USER_NAME, PASSWORD, EMAIL_ADDRESS, IMEI, ENCRYPTION_KEY, LAST_ONLINE, LAST_LOGIN,TOKEN,SOURCE_CODE,AO_TYPE) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)", params);
	}	
	
	public static long add(SQLiteDatabase db, UserInfo info) {
		ContentValues values = createContentValues(info);				
		return db.insert("TBL_USER", null, values);
	}	

	public void update(UserInfo info) {
		ContentValues values = createContentValues(info);

		SQLiteDatabase db = _db.getWritableDatabase();

		db.update("TBL_MS_USER", values, "USER_ID = ?",
				new String[] { String.valueOf(info.getId()) });
	}
	
	public void delete(long id) {		
		SQLiteDatabase db = _db.getWritableDatabase();

		db.delete("TBL_MS_USER", "USER_ID = ?", 
				new String[] { String.valueOf(id) });
	}	
	
	public Date updateLoginInfo(long id, boolean isOnline) {		
		SQLiteDatabase db = _db.getWritableDatabase();
		
		ContentValues values = new ContentValues();		
		Date currentDate = new Date();
		String formatted = formatFullDate(currentDate);
		if (isOnline)
			values.put("LAST_ONLINE", formatted);
		
		values.put("LAST_LOGIN", formatted);		
		
		db.update("TBL_MS_USER", values, "USER_ID = ?", 
				new String[] { String.valueOf(id) });
		return currentDate;
	}
	
	public UserInfo get(long id) {
		SQLiteDatabase db = _db.getReadableDatabase();

		Cursor cursor = db.rawQuery("SELECT * FROM TBL_MS_USER WHERE USER_ID = ?",
				new String[] { String.valueOf(id) });

		if (cursor != null && cursor.getCount() > 0)
			cursor.moveToFirst();
		else {
			cursor.close();
			return null;
		}

		UserInfo info = createObject(cursor);		
		cursor.close();

		return info;
	}
	
	public UserInfo get(String nip) {
		SQLiteDatabase db = _db.getReadableDatabase();

		Cursor cursor = db.rawQuery("SELECT * FROM TBL_MS_USER WHERE NIP = ?",
				new String[] { nip });

		if (cursor != null && cursor.getCount() > 0)
			cursor.moveToFirst();
		else {
			cursor.close();
			return null;
		}

		UserInfo info = createObject(cursor);		
		cursor.close();

		return info;
	}
	
	private UserInfo createObject(Cursor cursor) {
		UserInfo info = new UserInfo();
		info.setId(cursor.getLong(cursor.getColumnIndex("USER_ID")));
		info.setAoCode(cursorGetString(cursor, "AO_CODE"));
		info.setNip(cursorGetString(cursor, "NIP"));
		info.setUnitCode(cursorGetString(cursor, "UNIT_CODE"));
		info.setUserName(cursorGetString(cursor, "USER_NAME"));
		info.setEmailAddress(cursorGetString(cursor, "EMAIL_ADDRESS"));
		info.setPassword(cursorGetString(cursor, "PASSWORD"));
		info.setEncryptionKey(cursorGetString(cursor, "ENCRYPTION_KEY"));	
		info.setImei(cursorGetString(cursor, "IMEI"));
		info.setLastOnline(cursorGetDate(cursor, "LAST_ONLINE"));
		info.setLastLogin(cursorGetDate(cursor, "LAST_LOGIN"));
		info.setToken(cursorGetString(cursor, "TOKEN"));
		info.setSourceCode(cursorGetString(cursor, "SOURCE_CODE"));
		info.setAoType(cursorGetString(cursor, "AO_TYPE"));
		
		return info;
	}			
}
